﻿#region Copyright(C) 2012 NCS Pte. Ltd. All rights reserved.

// ==================================================================================================
// Copyright(C) 2012 NCS Pte. Ltd. All rights reserved.
//
// SYSTEM NAME	:   iConnect.Azure Application Framework
// COMPONENT ID :   NCS.IConnect.Azure.Diagnostics.AzureStorage.AzureStorageCloudBlob
// COMPONENT DESC:  	
//
// CREATED DATE/BY:  08 June 2012 / Lu Ya Ming
//
// REVISION HISTORY:	
// DATE/BY			SR#/CS/PM#/OTHERS		DESCRIPTION OF CHANGE
// ==================================================================================================

#endregion

using System;

using Microsoft.WindowsAzure.StorageClient;

namespace NCS.IConnect.Cloud.Core.AzureStorage
{
    /// <summary>
    /// The Azure storage cloud blob.
    /// </summary>
    public class AzureStorageCloudBlob : ICloudBlob
    {
        private readonly CloudBlob blob;
        private readonly ICloudBlobClient serviceClient;
        private readonly ICloudBlobContainer container;

        /// <summary>
        /// Initializes a new instance of the <see cref="AzureStorageCloudBlob"/> class.
        /// </summary>
        /// <param name="blob">The BLOB.</param>
        public AzureStorageCloudBlob(CloudBlob blob)
        {
            this.blob = blob;
            this.serviceClient = new AzureStorageCloudBlobClient(this.blob.ServiceClient);
            this.container = new AzureStorageCloudBlobContainer(this.blob.Container);
        }

        /// <summary>
        /// Gets the service client.
        /// </summary>
        /// <value>
        /// The service client.
        /// </value>
        public ICloudBlobClient ServiceClient
        {
            get { return this.serviceClient; }
        }

        /// <summary>
        /// Gets the container.
        /// </summary>
        /// <value>
        /// The container.
        /// </value>
        public ICloudBlobContainer Container
        {
            get { return this.container; }
        }

        /// <summary>
        /// Gets the URI.
        /// </summary>
        /// <value>
        /// The URI.
        /// </value>
        public Uri Uri
        {
            get { return this.blob.Uri; }
        }

        /// <summary>
        /// Gets the properties.
        /// </summary>
        /// <value>
        /// The properties.
        /// </value>
        public AzureBlobProperties Properties
        {
            get
            {
                // TODO: should re-use instance? consider if this.blob.Properties could change.
                return new AzureBlobProperties(this.blob.Properties);
            }
        }

        /// <summary>
        /// Downloads the text.
        /// </summary>
        /// <param name="options">The options.</param>
        /// <returns></returns>
        public string DownloadText(BlobRequestOptions options = null)
        {
            return this.blob.DownloadText(options);
        }

        /// <summary>
        /// Uploads the text.
        /// </summary>
        /// <param name="content">The content.</param>
        public void UploadText(string content)
        {
            this.blob.UploadText(content);
        }

        /// <summary>
        /// Fetches the attributes.
        /// </summary>
        /// <param name="options">The options.</param>
        public void FetchAttributes(BlobRequestOptions options = null)
        {
            this.blob.FetchAttributes(options);
        }
    }
}
